package com.csu.jpetstore_reconfiguration_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import com.csu.jpetstore_reconfiguration_back.domain.Item;
import com.csu.jpetstore_reconfiguration_back.domain.Order;
import com.csu.jpetstore_reconfiguration_back.domain.Orderlines;
import com.csu.jpetstore_reconfiguration_back.domain.User;
import com.csu.jpetstore_reconfiguration_back.mapper.ItemMapper;
import com.csu.jpetstore_reconfiguration_back.mapper.OrderlinesMapper;
import com.csu.jpetstore_reconfiguration_back.service.OrderService;
import com.csu.jpetstore_reconfiguration_back.mapper.OrderMapper;
import com.csu.jpetstore_reconfiguration_back.util.JwtUtil;
import com.csu.jpetstore_reconfiguration_back.vo.OrderVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* @author 朱嘉辉
* @description 针对表【t_order】的数据库操作Service实现
* @createDate 2024-04-13 20:36:36
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderlinesMapper orderlinesMapper;
    @Resource
    private ItemMapper itemMapper;

    @Override
    public CommonResponse<List<OrderVO>> getAllOrders(HttpServletRequest request)
    {
        String token=request.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        List<Order> orders = orderMapper.selectList(queryWrapper);
        if(orders.isEmpty())
        {
            return CommonResponse.createForError("没有订单");
        }
        List<OrderVO> orderVOS=new ArrayList<>();
        int n=0;
        for(Order order:orders)
        {
            OrderVO orderVO=new OrderVO();
            OtoOl(orders.get(n),orderVO);
            QueryWrapper<Orderlines> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("order_id",order.getOrderId());
            List<Orderlines> orderlines = orderlinesMapper.selectList(queryWrapper1);
            orderVO.setOrderLines(orderlines);
            orderVOS.add(orderVO);
            n++;
        }
        return CommonResponse.createForSuccess("查询成功",orderVOS);
    }

    @Override
    public  CommonResponse<OrderVO> getOrder(String id)
    {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",id);
        Order order = (Order) orderMapper.selectOne(queryWrapper);
        if(order==null){
            return CommonResponse.createForError("订单不存在");
        }
        OrderVO orderVO=new OrderVO();
        OtoOl(order,orderVO);
        QueryWrapper<Orderlines> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("order_id",id);
        List<Orderlines> orderlines = orderlinesMapper.selectList(queryWrapper1);
        orderVO.setOrderLines(orderlines);
        return CommonResponse.createForSuccess("查询成功",orderVO);
    }

    @Override
    public CommonResponse<OrderVO> addOrder(OrderVO orderVo,HttpServletRequest request)
    {
        String token=request.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        QueryWrapper<Order> queryWrapper0 = new QueryWrapper<>();
        queryWrapper0.orderByDesc("order_id");
        queryWrapper0.last("limit 1");
        Order order0 = orderMapper.selectOne(queryWrapper0);
        if(order0==null)
        {
            order0=new Order();
            order0.setOrderId(999);
        }
        orderVo.setOrderId(order0.getOrderId()+1);
        orderVo.setUserId(String.valueOf(user.getUserId()));
        orderVo.setOrderDate(now);
        orderVo.setStatus("OK");
        orderVo.setLineNum(orderVo.getOrderLines().size());
        Order order=new Order();
        OltoO(order,orderVo);


        orderMapper.insert(order);
        for(Orderlines orderlines:orderVo.getOrderLines())
        {
            QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("item_id",orderlines.getItemId());
            Item item = itemMapper.selectOne(queryWrapper);
            orderlines.setUnitPrice(item.getListPrice());
            orderlines.setOrderId(orderVo.getOrderId());
            orderlinesMapper.insert(orderlines);
        }
        return CommonResponse.createForSuccess("添加成功",orderVo);
    }

    @Override
    public CommonResponse<OrderVO> updateOrder(String id,OrderVO orderVO)
    {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",id);
        Order order=orderMapper.selectOne(queryWrapper);
        if(order==null)
        {
            return CommonResponse.createForError("订单不存在");
        }
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        orderVO.setOrderId(Integer.parseInt(id));
        orderVO.setUserId(order.getUserId());
        orderVO.setOrderDate(now);
        orderVO.setStatus("OK");
        orderVO.setLineNum(orderVO.getOrderLines().size());
        OltoO(order,orderVO);
        orderMapper.update(order,queryWrapper);
        QueryWrapper<Orderlines> queryWrapper0 = new QueryWrapper<>();
        queryWrapper0.eq("order_id",orderVO.getOrderId());
        orderlinesMapper.delete(queryWrapper0);
        for(Orderlines orderlines:orderVO.getOrderLines())
        {
            QueryWrapper<Item> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("item_id",orderlines.getItemId());
            Item item = itemMapper.selectOne(queryWrapper1);
            orderlines.setUnitPrice(item.getListPrice());
            orderlines.setOrderId(orderVO.getOrderId());
            orderlinesMapper.insert(orderlines);
        }
        return CommonResponse.createForSuccess("更新成功",orderVO);
    }

    @Override
    public CommonResponse<OrderVO> deleteOrder(String id)
    {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",id);
        Order order = (Order) orderMapper.selectOne(queryWrapper);
        if(order==null)
        {
            return CommonResponse.createForError("订单不存在");
        }
        OrderVO orderVO=new OrderVO();
        OtoOl(order,orderVO);
        QueryWrapper<Orderlines> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("order_id",id);
        List<Orderlines> orderlines = orderlinesMapper.selectList(queryWrapper1);
        orderVO.setOrderLines(orderlines);
        orderlinesMapper.delete(queryWrapper1);
        orderMapper.delete(queryWrapper);
        return CommonResponse.createForSuccess("删除成功",orderVO);
    }

    public void OltoO(Order order,OrderVO orderVo)
    {
        order.setUserId(orderVo.getUserId());
        order.setOrderId(orderVo.getOrderId());
        order.setOrderDate(orderVo.getOrderDate());
        order.setLineNum(orderVo.getLineNum());
        order.setStatus(orderVo.getStatus());
        order.setShipAddr1(orderVo.getShipAddr1());
        order.setShipAddr2(orderVo.getShipAddr2());
        order.setShipCity(orderVo.getShipCity());
        order.setShipState(orderVo.getShipState());
        order.setShipZip(orderVo.getShipZip());
        order.setShipCountry(orderVo.getShipCountry());
        order.setBillAddr1(orderVo.getBillAddr1());
        order.setBillAddr2(orderVo.getBillAddr2());
        order.setBillCity(orderVo.getBillCity());
        order.setBillState(orderVo.getBillState());
        order.setBillZip(orderVo.getBillZip());
        order.setBillCountry(orderVo.getBillCountry());
        order.setCourier(orderVo.getCourier());
        order.setTotalPrice(orderVo.getTotalPrice());
        order.setBillToFirstname(orderVo.getBillToFirstname());
        order.setBillToLastname(orderVo.getBillToLastname());
        order.setShipToFirstname(orderVo.getShipToFirstname());
        order.setShipToLastname(orderVo.getShipToLastname());
        order.setCreditCard(orderVo.getCreditCard());
        order.setExprDate(orderVo.getExprDate());
        order.setCardType(orderVo.getCardType());
        order.setLocale(orderVo.getLocale());
    }

    public void OtoOl(Order order,OrderVO orderVO)
    {
        orderVO.setUserId(order.getUserId());
        orderVO.setOrderId(order.getOrderId());
        orderVO.setOrderDate(order.getOrderDate());
        orderVO.setLineNum(order.getLineNum());
        orderVO.setStatus(order.getStatus());
        orderVO.setShipAddr1(order.getShipAddr1());
        orderVO.setShipAddr2(order.getShipAddr2());
        orderVO.setShipCity(order.getShipCity());
        orderVO.setShipState(order.getShipState());
        orderVO.setShipZip(order.getShipZip());
        orderVO.setShipCountry(order.getShipCountry());
        orderVO.setBillAddr1(order.getBillAddr1());
        orderVO.setBillAddr2(order.getBillAddr2());
        orderVO.setBillCity(order.getBillCity());
        orderVO.setBillState(order.getBillState());
        orderVO.setBillZip(order.getBillZip());
        orderVO.setBillCountry(order.getBillCountry());
        orderVO.setCourier(order.getCourier());
        orderVO.setTotalPrice(order.getTotalPrice());
        orderVO.setBillToFirstname(order.getBillToFirstname());
        orderVO.setBillToLastname(order.getBillToLastname());
        orderVO.setShipToFirstname(order.getShipToFirstname());
        orderVO.setShipToLastname(order.getShipToLastname());
        orderVO.setCreditCard(order.getCreditCard());
        orderVO.setExprDate(order.getExprDate());
        orderVO.setCardType(order.getCardType());
        orderVO.setLocale(order.getLocale());
    }
}




